<extend name="Public/base" />
<block name="style">
    <link rel="stylesheet" type="text/css" href="__STATIC__/bootstrap3/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="__STATIC__/FlatUI/css/flat-ui.min.css">
    <link rel="stylesheet" type="text/css" href="__STATIC__/style/style.css" >
</block>
<block name="body">

    <body>
        <div class="main">
            <div class="paper">
                <div class="toolbar">
                    <span class="title">下游管理</span>
                </div>
                <div class="divider"></div>
                <div class="body">
                    <div class="serch_area">
                        <button type="button" class="btn btn-embossed btn-info" data-toggle="modal" data-target="#addModal">
                                新增
                            </button>
                    </div>
                    <div class="divider"></div>
                    <div class="table_area content-block">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <th>id</th>
                                    <th>下游名称</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <volist name="lists" id="list">
                                    <tr>
                                        <td>{$list.id}</td>
                                        <td>{$list.name}</td>
                                        <td>
                                            <button type="button" class="btn btn-info btn-sm btn-embossed updatebtn" data-id="{$list.id}">编辑</button>
                                            <a type="button" class="btn btn-danger btn-sm btn-embossed" href="{:U('del',array('id'=>$list['id']))}">删除</a>
                                        </td>
                                    </tr>
                                </volist>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <!-- Modal -->
        <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h6 class="modal-title" id="myModalLabel">新增下游</h6>
                    </div>
                    <div class="modal-body">
                        <form id="add_form" class="form-horizontal public_form" action="{:U('add')}" method="post">
                            <div class="form-group">
                                <label for="exampleInputEmail1" class="col-lg-2 control-label">下游名称</label>
                                <div class="col-lg-8">
                                    <input type="text" class="form-control" id="exampleInputEmail1" name="name" placeholder="下游">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-embossed" data-dismiss="modal">关闭</button>
                        <button type="button" id="addbtn" class="btn btn-primary btn-embossed">保存</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- Modal -->
        <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h6 class="modal-title" id="myModalLabel">编辑下游</h6>
                    </div>
                    <div class="modal-body">
                        <form id="update_form" class="form-horizontal public_form" action="{:U('edit')}" method="post">
                            <div class="form-group">
                                <label for="exampleInputEmail1" class="col-lg-2 control-label">下游名称</label>
                                <div class="col-lg-8">
                                    <input type="text" class="form-control" id="exampleInputEmail1" name="name" placeholder="下游">
                                </div>
                            </div>
                            <input type="hidden" class="form-control" id="exampleInput" name="id">
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-embossed" data-dismiss="modal">关闭</button>
                        <button type="button" id="savetn" class="btn btn-primary btn-embossed">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </body>
</block>
<block name="script">
    <script type="text/javascript" src="__STATIC__/FlatUI/js/flat-ui.min.js"></script>
    <script type="text/javascript" src="__STATIC__/FlatUI/js/application.js"></script>
    <script type="text/javascript" src="__STATIC__/bootstrapValidator.min.js"></script>
    <script type="text/javascript" src="__STATIC__/validator.js"></script>
    <script>
        $(".public_form").submit(function (ev) {
            ev.preventDefault();
        });
        $("#addbtn").on("click", function () {
            var bootstrapValidator = $("#add_form").data('bootstrapValidator');
            bootstrapValidator.validate();
            if (bootstrapValidator.isValid()) {
                $("#add_form")[0].submit();
            } else {
                return;
            }
        })
        $("#savetn").on("click", function () {
            var bootstrapValidator = $("#update_form").data('bootstrapValidator');
            bootstrapValidator.validate();
            if (bootstrapValidator.isValid()) {
                $("#update_form")[0].submit();
            } else {
                return;
            }
        })
        $(".updatebtn").on("click", function (e) {
            e.stopPropagation();
            var id = $(this).attr("data-id");
            $.ajax({
                url: "{:U('edit')}",
                type: 'GET', //GET
                data: {
                    id: id
                },
                success: function (data) {
                    $("#update_form .form-control").each(function (index, val) {
                        var self = this;
                        for (x in data) {
                            if ($(self).attr("name") == x) {
                                $(self).val(data[x]);
                            }
                        }
                    });
                },
                async: false,
            })
            $('#updateModal').modal();
        })
    </script>
</block>